﻿using BotDetector.Core.Converter;
using BotDetector.Core.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using BotDetector.Core.Detector;

namespace BotDetector.Core.Search
{
    public class BingSearch : ISearch
    {
        
        private BotDetectorConfigurator configurator = new BotDetectorConfigurator();

        public ResultItemList Search(string phrase)
        {
            dynamic bing = new Bing.BingSearchContainer(
                            new Uri(configurator.urlBing))
                            {
                                Credentials = new System.Net.NetworkCredential(
                                                configurator.keyBing,
                                                configurator.keyBing)
                            };
            dynamic query = bing.Web(phrase, null, null, null, null, null, null, null);
            dynamic results = query.Execute();
            BingConverter c = new BingConverter();
            ResultItemList list = new ResultItemList() 
            {
                Phrase = phrase
            };
            foreach (var result in results)
            {
                ResultItem item = c.Convert(result);
                list.Add(item);
            }
            return list;
        }
    }
}
